/*
 * NumberSelectionView.java
 *
 * Created on August 25, 2006, 7:56 PM
 */
package com.floreantpos.ui.views;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import com.floreantpos.IconFactory;
import com.floreantpos.main.Application;
import com.floreantpos.swing.PosButton;
import com.floreantpos.swing.TransparentPanel;
import com.floreantpos.ui.dialog.POSMessageDialog;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MShahriar
 */
public class NumberSelectionView extends TransparentPanel implements ActionListener {

  private TitledBorder titledBorder;

  private boolean decimalAllowed;
  private JTextField tfNumber;

  private NumberFormat numberFormat = NumberFormat.getInstance(new Locale("pt", "BR"));
  private NumberFormat decimalFormat = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(new Locale("pt", "BR")));
  private NumberFormat intFormat = new DecimalFormat("#,##0", new DecimalFormatSymbols(new Locale("pt", "BR")));

  /**
   * Creates new form NumberSelectionView
   */
  public NumberSelectionView() {
    initComponents();
  }

  private void initComponents() {
    setLayout(new BorderLayout(5, 5));

    tfNumber = new JTextField();
    tfNumber.setText("0");
    tfNumber.setFont(tfNumber.getFont().deriveFont(Font.BOLD, 24));
    tfNumber.setEditable(false);
    tfNumber.setBackground(Color.WHITE);
    tfNumber.setHorizontalAlignment(JTextField.RIGHT);

    JPanel northPanel = new JPanel(new BorderLayout(5, 5));
    northPanel.add(tfNumber, BorderLayout.CENTER);

    PosButton btnClearAll = new PosButton();
    btnClearAll.setText(com.floreantpos.POSConstants.CLEAR_ALL);
    btnClearAll.setActionCommand(com.floreantpos.POSConstants.CLEAR_ALL);
    btnClearAll.setPreferredSize(new Dimension(90, 50));
    btnClearAll.addActionListener(this);
    northPanel.add(btnClearAll, BorderLayout.EAST);

    add(northPanel, BorderLayout.NORTH);

    String[][] numbers = {
      {"7", "8", "9"},
      {"4", "5", "6"},
      {"1", "2", "3"},
      {".", "0", com.floreantpos.POSConstants.CLEAR}
    };
    String[][] iconNames = new String[][]{
      {"7_32.png", "8_32.png", "9_32.png"},
      {"4_32.png", "5_32.png", "6_32.png"},
      {"1_32.png", "2_32.png", "3_32.png"},
      {"dot_32.png", "0_32.png", "clear_32.png"}
    };

    JPanel centerPanel = new JPanel(new GridLayout(4, 3, 5, 5));
    Dimension preferredSize = new Dimension(90, 80);

    for (int i = 0; i < numbers.length; i++) {
      for (int j = 0; j < numbers[i].length; j++) {
        PosButton posButton = new PosButton();
        ImageIcon icon = IconFactory.getIcon(iconNames[i][j]);
        String buttonText = String.valueOf(numbers[i][j]);

        if (icon == null) {
          posButton.setText(buttonText);
        } else {
          posButton.setIcon(icon);
        }

        posButton.setActionCommand(buttonText);
        posButton.setPreferredSize(preferredSize);
        posButton.addActionListener(this);
        centerPanel.add(posButton);
      }
    }
    add(centerPanel, BorderLayout.CENTER);

    titledBorder = new TitledBorder("");
    titledBorder.setTitleJustification(TitledBorder.CENTER);

    setBorder(titledBorder);
  }

  public void actionPerformed(ActionEvent e) {
    String actionCommand = e.getActionCommand();
    if (actionCommand.equals(com.floreantpos.POSConstants.CLEAR_ALL)) {
      tfNumber.setText("0");
    } else if (actionCommand.equals(com.floreantpos.POSConstants.CLEAR)) {
      String s = tfNumber.getText();
      if (s.length() > 1) {
        s = s.substring(0, s.length() - 1);
      } else {
        s = "0";
      }
      tfNumber.setText(s);
    } else if (actionCommand.equals(".")) {
      if (isDecimalAllowed() && tfNumber.getText().indexOf(',') < 0) {
        String string = tfNumber.getText() + ",";
        if (!validate(string)) {
          POSMessageDialog.showError(com.floreantpos.POSConstants.INVALID_NUMBER);
          return;
        }
        tfNumber.setText(string);
      }
    } else {
      String s = tfNumber.getText();
      if (s.equals("0")) {
        tfNumber.setText(actionCommand);
        return;
      }

      s = s + actionCommand;
      if (!validate(s)) {
        POSMessageDialog.showError(com.floreantpos.POSConstants.INVALID_NUMBER);
        return;
      }
      tfNumber.setText(Application.formatTextAsNumber(s));
    }

  }

  private boolean validate(String str) {
    if (isDecimalAllowed()) {
      try {
        numberFormat.parse(str).doubleValue();
      } catch (Exception x) {
        return false;
      }
    } else {
      try {
        numberFormat.parse(str).intValue();
      } catch (Exception x) {
        return false;
      }
    }
    return true;
  }

  public void setTitle(String title) {
    titledBorder.setTitle(title);
  }

  public double getValue() {
    try {
      return numberFormat.parse(tfNumber.getText()).doubleValue();
    } catch (ParseException ex) {
      Logger.getLogger(NumberSelectionView.class.getName()).log(Level.SEVERE, null, ex);
      return Double.valueOf(0);
    }
  }

  public void setValue(double value) {
    if (isDecimalAllowed()) {
      tfNumber.setText(decimalFormat.format(value));
    } else {
      tfNumber.setText(intFormat.format(value));
    }
  }

  public boolean isDecimalAllowed() {
    return decimalAllowed;
  }

  public void setDecimalAllowed(boolean decimalAllowed) {
    this.decimalAllowed = decimalAllowed;
  }
}
